<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="deptId" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-header>
				<div class="left-panel">
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="账号 / 姓名 / 电话" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :params="{isDisable:'all'}" :apiObj="apiObj" stripe remoteSort remoteFilter>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="用户账号" prop="accountId" width="150" sortable='custom'></el-table-column>
					<el-table-column label="App数字账号" prop="chatNumber" width="150"></el-table-column>
					<el-table-column label="用户姓名" prop="userName" width="100"></el-table-column>
					<el-table-column label="别名" prop="alias" width="100"></el-table-column>
					<el-table-column label="性别" prop="sex" width="60">
						<template #default="scope">
							<el-button type="primary" size="small" v-if="scope.row.sex==='1'">男</el-button>
							<el-button type="danger" size="small" v-else-if="scope.row.sex==='0'">女</el-button>
							<el-button type="info" size="small" v-else>保密</el-button>
						</template>
					</el-table-column>
					<el-table-column label="人员来源" prop="isIn" width="100">
						<template #default="scope">
							<el-button type="warning" size="small" round v-if="scope.row.isIn==='1'">外部</el-button>
							<el-button type="success" size="small" round v-else-if="scope.row.isIn==='0'">内部</el-button>
							<el-button type="info" size="small" round v-else>未知</el-button>
						</template>
					</el-table-column>
					<el-table-column label="职务" prop="postion" width="100"></el-table-column>
					<el-table-column label="手机号" prop="phone" width="150">
						<template #default="scope">
							{{tool.crypto.convertData(scope.row.phone)}}
						</template>
					</el-table-column>
					<el-table-column label="人员状态" prop="status" width="100">
						<template #default="scope">
							<el-tag type="success" effect="dark" v-if="scope.row.status=='0'">正常登陆</el-tag>
							<el-tag type="info" effect="dark" v-else>禁止登陆</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="账号状态" prop="isDisable" width="100">
						<template #default="scope">
							<el-tag type="success" effect="dark" v-if="scope.row.isDisable=='0'">启用</el-tag>
							<el-tag type="danger" effect="dark" v-else-if="scope.row.isDisable=='1'">禁用</el-tag>
							<el-tag type="info" effect="dark" v-else>未开通</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="150">
						<template #default="scope">
							<el-button-group>
								<el-popconfirm title="确定重置密码吗？" @confirm="reSetPassword(scope.row, scope.$index)" v-if="scope.row.isDisable=='0'">
									<template #reference>
										<el-button text type="primary" size="small">密码</el-button>
									</template>
								</el-popconfirm>
								<el-popconfirm title="确定禁用吗？" @confirm="setStopLogin(scope.row, scope.$index)" v-if="scope.row.isDisable=='0'">
									<template #reference>
										<el-button text type="primary" size="small" v-if="scope.row.status=='0'">禁用</el-button>
										<el-button text type="primary" size="small" v-else>启用</el-button>
									</template>
								</el-popconfirm>
								<el-popconfirm title="确定启用吗？" @confirm="setOpenLogin(scope.row, scope.$index)" v-else-if="scope.row.isDisable=='1'">
									<template #reference>
										<el-button text type="primary" size="small">启用</el-button>
									</template>
								</el-popconfirm>
								<el-popconfirm title="确定开通吗？" @confirm="addChatAccount(scope.row, scope.$index)" v-else>
									<template #reference>
										<el-button text type="primary" size="small" v-if="scope.row.status=='0'">开通</el-button>
										<el-button text type="primary" size="small" v-else>启用</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'
import scSelectFilter from '@/components/scSelectFilter'
import tool from "../../../../utils/tool";
export default {
	name: 'App User Manage',
	computed: {
		tool() {
			return tool
		}
	},
	components: {
		scSelectFilter,
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			groupsProps: {
				label:"deptName",
				value: "deptId"
			},
			apiObj: this.$API.chat.user.getChatUserInfoByDeptId,
			filterData: [
				{
					title: "当前状态",
					key: "isDisable",
					multiple: false,
					options: [{
						value: "all",
						label: '全部'
					},{
						value: '',
						label: '未开通'
					},{
						value: '0',
						label: '正常'
					},{
						value: '1',
						label: '已停用'
					}],
				}
			],
			search: {
				isDisable:"all",
				keyword: null
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getDeptGroup()
	},
	methods: {
		filterChange(data){
			this.search.isDisable = data.isDisable;
			this.$refs.table.upData(this.search)
		},
		reSetPassword(row,index){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().setData(row)
			})
		},
		async addChatAccount(row, index)
		{
			let res = await this.$API.chat.user.addChatAccount.post({accountId:row.accountId});
			if (res.code == 200) {
				this.$refs.table.refresh()
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}

		},
		async setStopLogin(row, index)
		{
			let res = await this.$API.chat.user.setStopLogin.post({accountId:row.accountId});
			if (res.code == 200) {
				this.$refs.table.refresh()
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async setOpenLogin(row, index)
		{
			let res = await this.$API.chat.user.setOpenLogin.post({accountId:row.accountId});
			if (res.code == 200) {
				this.$refs.table.refresh()
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//加载树数据
		async getDeptGroup(){
			this.showGrouploading = true;
			var res = await this.$API.system.unitDept.list.get();
			this.showGrouploading = false;
			var allNode ={deptId: '', deptName: '所有'}
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.deptName.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			this.search.deptId = data.deptId
			this.$refs.table.upData(this.search)
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode){
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
